package com.tiancikeji.zaoke.gui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JTextArea;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.javagui.swing.components.Table;
import org.javagui.swing.components.TextField;

import com.tiancikeji.zaoke.gui.Form;
import com.tiancikeji.zaoke.gui.http.HttpLoader;
import com.tiancikeji.zaoke.gui.http.Loader;
import com.tiancikeji.zaoke.gui.model.order.Order;

public class ScannFilterListener implements ActionListener {

	private long zoneId;
	
	private TextField txtBno;
	
	private Table<Order> table;
	
	private Loader loader;
	
	private JTextArea textArea;
	
	private String token_state = HttpLoader.getState(Order.STATE_TOKEN + "");

	public void refresh(){
		List<Order> orders = loader.loadOrdersByZoneId(zoneId);
		Map<String, Integer> tmp = new HashMap<String, Integer>();
		Map<String, Integer> all = new HashMap<String, Integer>();
		if(orders != null && orders.size() > 0){
			table.removeAll();
			for(Order order : orders){
				table.addRow(order);
				int c = 0;
				if(!StringUtils.equals(token_state, order.getState()))
					c = 1;
				if(order.getMfoodid() > 0){
					Integer cnt = tmp.get(order.getMfoodName());
					if(cnt == null)
						cnt = 0;
					tmp.put(order.getMfoodName(), cnt + c);
					cnt = all.get(order.getMfoodName());
					if(cnt == null)
						cnt = 0;
					all.put(order.getMfoodName(), cnt + 1);
				}
				if(order.getSfoodid() > 0){
					Integer cnt = tmp.get(order.getSfoodName());
					if(cnt == null)
						cnt = 0;
					tmp.put(order.getSfoodName(), cnt + c);
					cnt = all.get(order.getSfoodName());
					if(cnt == null)
						cnt = 0;
					all.put(order.getSfoodName(), cnt + 1);
				}
			}
		}

		StringBuilder sb = new StringBuilder();
		if(tmp.size() > 0)
			for(String key : tmp.keySet()){
				sb.append(key);
				sb.append(" : ");
				sb.append(tmp.get(key));
				sb.append("/");
				sb.append(all.get(key));
				sb.append("\n");
			}
		textArea.setText(sb.toString());
		return;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String strId = StringUtils.trimToEmpty(txtBno.getText());
		
		if(StringUtils.isBlank(strId)){
			refresh();
			return;
		}
		
		int size = strId.length();
		if(NumberUtils.isNumber(strId) && (size == 11 || size == 8 || size >= 12)){
			Long id = NumberUtils.toLong(strId);
			
			List<Order> tmps = new ArrayList<Order>();
			List<Order> orders = loader.loadOrdersByZoneId(zoneId);
			
			if(orders != null && orders.size() > 0 && id > 0)
				for(Order order : orders)
					if((size == 8 && order.getCardId() == id) || (size == 11 && StringUtils.equals(order.getPhone(), strId)) || order.getUserId() == id)
						tmps.add(order);
			
			if(tmps != null && tmps.size() > 0){
				//table.removeAll();
				for(int i = 0; i < tmps.size(); i ++){
					Order order = tmps.get(i);
					//table.addRow(order);
					PickFoodFormListener listener = new PickFoodFormListener();
					listener.setLoader(loader);
					listener.setOrder(order);
					Form form = new Form(order, listener, i);
					listener.setFrame(form.getFrame());
					listener.setTable(this);
					form.getFrame().setVisible(true);
				}
			}
		} /*else
			table.removeAll();*/
	}

	public long getZoneId() {
		return zoneId;
	}

	public void setZoneId(long zoneId) {
		this.zoneId = zoneId;
	}

	public TextField getTxtBno() {
		return txtBno;
	}

	public void setTxtBno(TextField txtBno) {
		this.txtBno = txtBno;
	}

	public Table<Order> getTable() {
		return table;
	}

	public void setTable(Table<Order> table) {
		this.table = table;
	}

	public Loader getLoader() {
		return loader;
	}

	public void setLoader(Loader loader) {
		this.loader = loader;
	}
	
	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}
	
	public JTextArea getTextArea() {
		return textArea;
	}
}
